package com.room.rest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cyh.dao.SysConstant;
import com.cyh.dao.entity.*;
import com.cyh.dao.mapper.ReserveMapper;
import com.room.core.annotation.LoginUser;
import com.room.core.dto.UserInfo;
import com.room.core.util.R;
import com.room.service.*;
import com.room.vo.ReserveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SolicitDmtService dmtService;

    @Autowired
    private SolicitNewService newService;
    
    @Autowired
    private ReportService reportService;

    /**
     * 获取排名列表
     * @param userInfo
     * @return
     */
    @PostMapping("getRankList")
    public R getRankList(@LoginUser UserInfo userInfo) {
        List<SysUser> list = sysUserService.getRankList();
        return R.success(list);
    }

    /**
     * 学生申请记录
     * @param userInfo
     * @return
     */
    @PostMapping("getSolicitrRecord")
    public R getSolicitrRecord(@LoginUser UserInfo userInfo) {
        Map<String, Object> map = sysUserService.getSolicitrRecord(userInfo);
        return R.success(map);
    }

    /**
     * 申请多媒体
     * @param userInfo
     * @param dmt
     * @return
     */
    @PostMapping("submitDmt")
    public R submitDmt(@LoginUser UserInfo userInfo, @RequestBody SolicitDmt dmt) {
        dmt.setUserId(userInfo.getId());
        dmtService.save(dmt);
        return R.success();
    }


    /**
     * 申请新教室
     * @param userInfo
     * @return
     */
    @PostMapping("submitNew")
    public R submitNew(@LoginUser UserInfo userInfo) {
        List<SolicitNew> list = newService.list(new LambdaQueryWrapper<SolicitNew>()
                .eq(SolicitNew::getUserId, userInfo.getId())
                .eq(SolicitNew::getAgreeIs, 0));
        if(CollectionUtils.isEmpty(list)){
            SolicitNew solicitNew = SolicitNew.builder()
                    .agreeIs(0)
                    .userId(userInfo.getId())
                    .build();
            newService.save(solicitNew);
            return R.success();
        }else {
            return R.fail("已申请,请等待审核!");
        }
    }

    /**
     * 获取排名
     * @param userInfo
     * @return
     */
    @PostMapping("getMyLearnData")
    public R getMyRank(@LoginUser UserInfo userInfo) {
        Integer learnLen = 0;
        Integer rank = 0;
        List<SysUser> list = sysUserService.list(new LambdaQueryWrapper<SysUser>()
                .orderByDesc(SysUser::getLearnLen));
        for (int i=0; i<list.size(); i++) {
            SysUser user = list.get(i);
            if (user.getId().equals(userInfo.getId())){
                rank = i+1;
                break;
            }
        }

        Map<String,Integer> r = new HashMap<>();
        r.put("learnLen",sysUserService.getOne(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getId,userInfo.getId())).getLearnLen());
        r.put("rank", rank);
        return R.success(r);
    }


    @PostMapping("isReport")
    public R isReport(@LoginUser UserInfo userInfo) {
        Report one = reportService.getOne(new LambdaQueryWrapper<Report>()
                .eq(Report::getReportUserId, userInfo.getId())
                .eq(Report::getReportUserBack, 0));
        int count = reportService.count(new LambdaQueryWrapper<Report>()
                .eq(Report::getReportUserId, userInfo.getId())
                .eq(Report::getReportUserBack, 2));
        Map<String,Object> r = new HashMap<>();
        r.put("report",one);
        r.put("count",count);
        return R.success(r);
    }


    @Autowired
    private ReserveMapper reserveMapper;
    @Autowired
    private ReserveService reserveService;

    @PostMapping("myGetBack")
    public R myGetBack(@LoginUser UserInfo userInfo) {
        Reserve reserve = reserveMapper.getMyReserve(userInfo.getId());
        return R.success(reserve);
    }


    @PostMapping("submitBack")
    @Transactional(rollbackFor = Exception.class)
    public R submitBack(@LoginUser UserInfo userInfo, @RequestParam("id") Long id) {
        Reserve reserve = reserveService.getById(id);
        reserve.setInvalidIs(1);
        reserveService.updateById(reserve);

        SysUser user = sysUserService.getById(userInfo.getId());
        LocalDateTime now = LocalDateTime.now();
        Integer hour =0;
        if (reserve.getTimeInterval().equals(SysConstant.SJD_A)){
            hour = 8;

        }else if (reserve.getTimeInterval().equals(SysConstant.SJD_B)){
            hour = 13;

        }else if (reserve.getTimeInterval().equals(SysConstant.SJD_C)){
            hour = 19;
        }
        LocalDateTime of = LocalDateTime.of(now.getYear(), now.getMonth().getValue(),
                now.getDayOfMonth(), hour, 0);
        Duration between = Duration.between(of, now);
        user.setLearnLen(user.getLearnLen() + Integer.valueOf(between.toHours()+""));
        sysUserService.updateById(user);
        return R.success();
    }

}
